/*
Disclaimer of Warranty: THE PACKAGE IS PROVIDED
BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS'
AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT
ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW.
UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF
THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


(c) Copyright 2013 Edward Principe
All Rights Reserved

*/
#include "stdafx.h"
#include "tokline.h"

///////////////////////////////////////////////////////////////
// Append: Appends tokens to a line of tokens
///////////////////////////////////////////////////////////////
void TokLine::Append(const TokLine& rhs)
{
    for(list<TokNode>::const_iterator i = rhs._tokens.begin(); i != rhs._tokens.end(); i++)
        _tokens.push_back(*i);

    _line = rhs._line;
}

void TokLine::Append(const TokNode& rhs)
{
    _tokens.push_back(rhs);
}

//////////////////////////////////////////////////////////////////
// Set: Adds tokens
/////////////////////////////////////////////////////////////////
void TokLine::Set(const TokLine& tokens)
{
    for(list<TokNode>::const_iterator i = tokens._tokens.begin(); i != tokens._tokens.end(); i++)
        _tokens.push_back(*i);
}

void TokLine::Set(const TokNode& token)
{
    _tokens.push_back(token);
}

///////////////////////////////////////////////////////////////////
// Write: Writes tokens
//////////////////////////////////////////////////////////////////
void TokLine::Write(fstream& out)
{
    // Get count
    int count = 0;

    for(list<TokNode>::iterator i = _tokens.begin(); i != _tokens.end(); i++)
        count++;

    // Write line #
    if(!out.write(reinterpret_cast<char *>(&_line), sizeof(_line)))
            throw new CantWriteFile();

    // Write token counter
    if(!out.write(reinterpret_cast<char *>(&count), sizeof(count)))
        throw new CantWriteFile();

    // Write tokens
    for(list<TokNode>::iterator i=_tokens.begin(); i!=_tokens.end(); i++)
        i->Write(out);
}

///////////////////////////////////////////////////////////////////////////////
// Read: Reads a line of tokens
///////////////////////////////////////////////////////////////////////////////
void TokLine::Read(fstream& in)
{
    int count;
    SmtPtr<TokNode> token;

    // Erase current tokens
    _tokens.erase(_tokens.begin(), _tokens.end());

    // Read line #
    if(!in.read(reinterpret_cast<char *>(&_line), sizeof(_line)))
        throw new CantWriteFile();

    // Read token counter
    if(!in.read(reinterpret_cast<char *>(&count), sizeof(count)))
        throw new CantWriteFile();

    // Load tokens
    for(int i=0; i<count; i++)
    {
        // Read buffer
        token->Read(in);

        // Add token
        _tokens.push_back(*token);
    }
}

/////////////////////////////////////////////////////////////
// Dump: Dumps line of tokens
/////////////////////////////////////////////////////////////
#ifdef _DEBUG
void TokLine::Dump()
{
	cout << "LINE: " << _line << endl;

	for (list<TokNode>::iterator i = _tokens.begin(); i != _tokens.end(); i++)
		i->Dump();
}
#endif